/**
 * 分组分类排序
 * @param arr
 * @param label
 * @returns arr
 */
export function groudSort(arr: any[], label: string) {
  const map: any = {}
  const myArr = []
  for (let i = 0; i < arr.length; i++) {
    if (!map[arr[i][label]]) {
      myArr.push({
        [label]: arr[i][label],
        data: [arr[i]]
      })
      map[arr[i][label]] = arr[i]
    } else {
      for (let j = 0; j < myArr.length; j++) {
        if (arr[i][label] === myArr[j][label]) {
          myArr[j].data.push(arr[i])
          break
        }
      }
    }
  }
  return myArr
}
// 公共延时器时间
export const timeNum = 30 * 60 * 1000
// 公共设备对应字段
export const deviceObj: any = {
  '1': '心电设备',
  '2': '血压设备',
  '3': '血糖设备',
  '4': '睡眠监测设备',
  '5': '防跌倒监测设备',
  '6': '血氧设备'
}
// 丝滑滚动
export const smoothScroll = (start: any, target: any, scrollContainer: any) => {
  const duration = 500
  let startTime: number

  const animateScroll = (timestamp: number) => {
    if (!startTime) startTime = timestamp
    const progress = timestamp - startTime
    const easeInOutCubic = (progress: number) =>
      progress < 0.5 ? 4 * progress ** 3 : 1 - (-2 * progress + 2) ** 3 / 2
    const easing = easeInOutCubic(progress / duration)

    const currentScroll = start + (target - start) * easing
    scrollContainer.scrollTop = currentScroll

    if (progress < duration) {
      requestAnimationFrame(animateScroll)
    }
  }

  requestAnimationFrame(animateScroll)
}
// 姓名脱敏
export function nameHide(name: string) {
  if (name.length == 2) {
    name = name.substring(0, 1) + '*' // 截取name 字符串截取第一个字符，
    return name // 张三显示为张*
  } else if (name.length == 3) {
    name = name.substring(0, 1) + '*' + name.substring(2, 3) // 截取第一个和第三个字符
    return name // 李思思显示为李*思
  } else if (name.length > 3) {
    name = name.substring(0, 1) + '*' + '*' + name.substring(3, name.length) // 截取第一个和大于第4个字符
    return name // 王二麻子显示为王**子
  }
}
